# # !vipermake-bootstrap
# # TODO: ^bootstrap needs to check for this indicator
# import sys
# import platform
# from os import environ, system
# from os.path import exists
# 
# tasks = {}
# first_task_name = None
# default_task_name = None
# if platform.system() == "Windows":
#     makebin = "nmake"
# else:
#     makebin = "make"
# 
# def task(method=None, default=False):
#     def decorate(method, default):
# 
#         global first_task_name
#         global default_task_name
#         
#         if first_task_name == None:
#             first_task_name = method.__name__
#         
#         if default == True:
#             default_task_name = method.__name__
#         
#         tasks[method.__name__] = method
#         return method
#         
#     if method == None:
#         def subdecorator(method):
#             return decorate(method, default)
#         return subdecorator
#     else:
#         return decorate(method, default)
# 
# def list_tasks():
#     Flag("noupdate").explain("skip Subversion Library updates")
#     print
#     for task in tasks.values():
#         format_string = "%%(name)-%ss # %%(doc)s" % max([len(name)+1 for name in tasks.keys()])
#         print format_string % dict(
#             name = task.__name__,
#             doc = task.__doc__,
#         )
#     print
#     for flagname, explanation in Flag._flag_explanation_lookup.iteritems():
#         print "[%(flagname)s=?] %(explanation)s" % locals()
#     print
# 
# def run_task(task_name):
#     if task_name in tasks:
#         tasks[task_name]()
#     else:
#         print >> sys.stderr, "*** there is no task with the name '%s'" % task_name
#         raise SystemExit(1)
# 
# def run_default_task():
#     global first_task_name
#     global default_task_name
#     
#     if default_task_name:
#         run_task(default_task_name)
#     
#     else:
#         run_task(first_task_name)
# 
# def run(cmd, quiet=False):
#     if not quiet:
#         print cmd
#     system(cmd)
# 
# def make(target, path):
#     global makebin
#     run("cd %(path)s && %(makebin)s %(target)s" % dict(
#         path = path,
#         makebin = makebin,
#         target = target,
#     ))
# 
# class Flag(object):
#     
#     _flag_value_lookup = None
#     _flag_explanation_lookup = {}
#     
#     def _get_value(self):
#         if not self.given:
#             print >> sys.stderr, "*** the Flag '%s' was not given" % self.__flag_name
#             raise SystemExit(1)
#         else:
#             return Flag._flag_value_lookup[self.__flag_name]
#     
#     given = property(lambda self: bool(self.__flag_name in Flag._flag_value_lookup))
#     value = property(_get_value)
#     explanation = property(lambda self: Flag._flag_explanation_lookup.get(self.__flag_name, None))
#     
#     def explain(self, explanation):
#
#         if self.__flag_name in Flag._flag_explanation_lookup.keys():
#         
#             # this Flag was already explained once, don't allow re-explanation
#             print >> sys.stderr, "*** the Flag '%(flagname)s' is already being used for '%(explanation)s" % dict(
#                 flagname = self.__flag_name,
#                 explanation = Flag._flag_explanation_lookup[self.__flag_name],
#             )
#             raise SystemExit(1)
#         
#         else:
#             
#             # no conflicts, set the explanation
#             Flag._flag_explanation_lookup[self.__flag_name] = explanation
#
#     def __init__(self, flag_name):
#         
#         # lazy-parse the makeflags
#         if Flag._flag_value_lookup == None:
#             if "MAKEFLAGS" not in environ:
#                 print >> sys.stderr, "*** MAKEFLAGS does not exist in the environment, did you try to call this outside of make?"
#                 raise SystemExit(1)
#             Flag._flag_value_lookup = {}
#             for key_and_value in environ["MAKEFLAGS"].split():
#                 key,value = key_and_value.split("=", 1)
#                 Flag._flag_value_lookup[key] = value
#         
#         # keep information about this flag
#         self.__flag_name = flag_name
# 
# 
# class Library(object):
#     
#     def __init__(self, url, path, build_cmd=None, clean_cmd=None):
# 
#         self.__url = url
#         self.__path = path
# 
#         if build_cmd:
#             self.__build_cmd = build_cmd
#         else:
#             self.__build_cmd = lambda: make("", self.__path)
# 
#         if clean_cmd:
#             self.__clean_cmd = clean_cmd
#         else:
#             self.__clean_cmd = lambda: make("clean", self.__path)
#     
#     def update(self):
#         if not Flag("noupdate").given or Flag("noupdate").value.lower() is not "true":
#             
#             # the 'noupdate' flag was not given, do an svn update
#             if not exists(self.__path):
#                 
#                 # checkout did not exist yet, check it out first
#                 run("svn co %(url)s %(path)s" % dict(
#                     url = self.__url,
#                     path = self.__path,
#                 ))
#                 
#             else:
#                 
#                 # checkout existed, do an svn update
#                 run("cd %(path)s && svn up" % dict(
#                     path = self.__path
#                 ))
#         else:
#             
#             # the 'noupdate' flag was given on the commandline, don't do subversion updates
#             print >> sys.stderr, "*** skipping Library Subversion updates"
#     
#     def build(self):
#         
#         # if we haven't done a checkout yet, do it
#         if not exists(self.__path):
#             self.update()
#         
#         if isinstance(self.__build_cmd, basestring):
#             
#             # the command was a string, exec it on the commandline
#             run("cd %(path)s && %(build_cmd)s" % dict(
#                 path = self.__path,
#                 build_cmd = self.__build_cmd,
#             ))
#             
#         else:
#             
#             # the command was a callback, use it
#             self.__build_cmd()
#     
#     def clean(self):
#         
#         # if we haven't done a checkout yet, do it
#         if not exists(self.__path):
#             self.update()
#             
#         if isinstance(self.__clean_cmd, basestring):
# 
#             # the command was a string, exec it on the commandline
#             run("cd %(path)s && %(clean_cmd)s" % dict(
#                 path = self.__path,
#                 clean_cmd = self.__clean_cmd,
#             ))
#             
#         else:
#             
#             # the command was a callback, use it
#             self.__clean_cmd()

__do_default_task:
	@python -c "import sys;from imp import load_source;from tempfile import TemporaryFile, gettempdir;vipermake_bootstrap_src=TemporaryFile();lines=filter(lambda line: line.startswith('#'), file('Makefile').readlines());[vipermake_bootstrap_src.write(line[2:]) for line in lines];vipermake_bootstrap_src.flush();vipermake_bootstrap_src.seek(0);vipermake = load_source('vipermake', gettempdir(), vipermake_bootstrap_src);sys.modules['vipermake']=vipermake;makefile_py = load_source('Makefile', '.', file('Makefile.py'));vipermake.run_default_task();"

ls:
	@python -c "import sys;from imp import load_source;from tempfile import TemporaryFile, gettempdir;vipermake_bootstrap_src=TemporaryFile();lines=filter(lambda line: line.startswith('#'), file('Makefile').readlines());[vipermake_bootstrap_src.write(line[2:]) for line in lines];vipermake_bootstrap_src.flush();vipermake_bootstrap_src.seek(0);vipermake = load_source('vipermake', gettempdir(), vipermake_bootstrap_src);sys.modules['vipermake']=vipermake;makefile_py = load_source('Makefile', '.', file('Makefile.py'));vipermake.list_tasks();"

%:
	@python -c "import sys;from imp import load_source;from tempfile import TemporaryFile, gettempdir;vipermake_bootstrap_src=TemporaryFile();lines=filter(lambda line: line.startswith('#'), file('Makefile').readlines());[vipermake_bootstrap_src.write(line[2:]) for line in lines];vipermake_bootstrap_src.flush();vipermake_bootstrap_src.seek(0);vipermake = load_source('vipermake', gettempdir(), vipermake_bootstrap_src);sys.modules['vipermake']=vipermake;makefile_py = load_source('Makefile', '.', file('Makefile.py'));vipermake.run_task('$@')"
